/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers.sysAdmin;

import controllers.utils.DatabaseHjelper;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import models.Department;
import models.Institute;

/**
 *
 * @author Ida
 */
@SessionScoped
@Named(value="newDepartment")
public class AddNewDepartment implements Serializable {
    private Department newDepartment;
    private DatabaseHjelper dbh = DatabaseHjelper.hent();
    private final List<Institute> institutes = new ArrayList<Institute>();

    public AddNewDepartment() {
        this.newDepartment = new Department();
        if(institutes.isEmpty()) updateInstitutes();
    }

    private void updateInstitutes() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("select * from institute");
            res = stmt.executeQuery();
            synchronized (institutes) {
                institutes.clear();
                while (res.next()){
                    Institute inst = new Institute();
                    inst.setInstituteID(res.getInt("instituteID"));
                    inst.setName(res.getString("instituteName"));
                    inst.setAddress(res.getString("instAddress"));
                    inst.setPhoneNr(res.getString("instPhoneNr"));
                    inst.setNote(res.getString("instNote"));
                    institutes.add(inst);
                }
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            DatabaseHjelper.lukk(res);
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

    public String addNewDepartment(){
        newDepartment.setName(null);
        newDepartment.setAddress(null);
        newDepartment.setPhoneNr(null);
        //newDepartment.getInstitute().setInstituteID(0);
        newDepartment.setNote(null);
        return "addNewDepartment";
    }

     public String executeNewDepartment(){
        addNewDepartmentToDB();
        return "addNewDepartmentOutput";
    }

      private void addNewDepartmentToDB() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("INSERT INTO department(departmentName, depAddress, depPhoneNr, depNote, instituteID) VALUES(?, ?, ?, ?, ?)");
            stmt.setString(1, newDepartment.getName());
            stmt.setString(2, newDepartment.getAddress());
            stmt.setString(3, newDepartment.getPhoneNr());
            stmt.setString(4, newDepartment.getNote());
            stmt.setInt(5, newDepartment.getInstitute().getInstituteID());
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Feil i lagring av bruker: " + e);
        } finally {
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

    public Department getNewDepartment() {
        return newDepartment;
    }

    public List<Institute> getInstitutes() {
        return institutes;
    }

}
